//
//  LYQPhoneLoginViewController.m
//  ZCProject
//
//  Created by Zheng on 16/11/15.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQPhoneLoginViewController.h"
#import "LYQLoginSettingPasswordViewController.h"
#import "LYQLoginNationListViewController.h"

#import "LYQLoginDefaultCell.h"

#import "LYQLoginModel.h"


@interface LYQPhoneLoginViewController () <UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIButton *forgetBtn; //忘记密码
@property (nonatomic, strong) UIButton *loginBtn; //登录
@property (nonatomic, strong) UIButton *registerBtn; //账号注册
@property (nonatomic, strong) LYQLoginModel *model;

@end

@implementation LYQPhoneLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"手机登录";
    
    _model = [[LYQLoginModel alloc] init];
    
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    self.listTableView.scrollEnabled = NO;
    self.listTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"login_bg2"]];
    
    [self.view addSubview:self.forgetBtn];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.registerBtn];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEditingTapAction)];
    tapGR.delegate = self;
    [self.view addGestureRecognizer:tapGR];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    UIBarButtonItem *backbutton = [[UIBarButtonItem alloc] init];
    [backbutton setTitleTextAttributes:@{NSFontAttributeName:kWFont(16)} forState:UIControlStateNormal];
    backbutton.title = @"返回";
    self.navigationItem.backBarButtonItem = backbutton;

    [_forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(kWScale(205));
        make.height.equalTo(@(kWScale(40)));
        make.width.equalTo(@(kWScale(100)));
    }];
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWScale(42));
        make.top.equalTo(_forgetBtn.mas_bottom).offset(kWScale(20));
        make.right.mas_equalTo(-kWScale(42));
        make.height.equalTo(@(kWScale(40)));
    }];
    
    [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(kWScale(100)));
        make.height.equalTo(@(kWScale(35)));
        make.bottom.mas_equalTo(-kWScale(30));
        make.centerX.equalTo(_loginBtn);
    }];
}

#pragma mark - load subviews
- (UIButton *)forgetBtn {
    if (!_forgetBtn) {
        _forgetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
        [_forgetBtn setTitleColor:kLYQBlue forState:UIControlStateNormal];
        _forgetBtn.titleLabel.font = kWFont(14);
        [_forgetBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetBtn;
}

- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _loginBtn.backgroundColor = kLYQBlue;
        _loginBtn.layer.cornerRadius = kWScale(20);
        _loginBtn.titleLabel.font = kWFont(16);
        [_loginBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _loginBtn;
}

- (UIButton *)registerBtn {
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerBtn setTitleColor:kLYQBlue forState:UIControlStateNormal];
        [_registerBtn setTitle:@"账号注册" forState:UIControlStateNormal];
        _registerBtn.backgroundColor = [UIColor whiteColor];
        _registerBtn.layer.cornerRadius = kWScale(17.5);
        _registerBtn.layer.borderColor = [kLYQBlue CGColor];
        _registerBtn.layer.borderWidth = kWScale(1);
        _registerBtn.titleLabel.font = kWFont(14);
        [_registerBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

#pragma mark - button action
- (void)buttonAction:(UIButton *)sender {
    
    if ([sender.titleLabel.text isEqualToString:@"忘记密码"]) {
        //忘记密码
        LYQLoginSettingPasswordViewController *lspVC = [[LYQLoginSettingPasswordViewController alloc] init];
        lspVC.navigationItem.title = @"重置密码";
        [self.navigationController pushViewController:lspVC animated:YES];
    } else if ([sender.titleLabel.text isEqualToString:@"登录"]) {
        
#pragma mark 登录
        
        if (_model.phoneStr.length != 11) {
            [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写正确的手机号码"];
            return;
        }
        
        if (_model.passwordStr.length < 6) {
            [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写正确的密码"];
            return;
        }
        
//        LYQUserInfoManager *manager = [LYQUserInfoManager defaultManager];
//        [manager updateUserInfo:@{@"phoneNumber":_model.phoneStr,
//                                 @"password":_model.passwordStr}];
        
        
        NSDictionary *param = @{@"moduleid":@"2",
                                @"action":@"login",
                                @"username":_model.phoneStr,
                                @"password":[_model.passwordStr md5String]};
        
        [NetRequest PostWithRequestStyle:NetRequestHostTypeStyleDefault withAPI:@"app/app.php" param:param success:^(id response) {
            
            
        } error:^(NSError *error) {
            
        }];
        
        
    } else if ([sender.titleLabel.text isEqualToString:@"账号注册"]) {
        //账号注册
        LYQLoginSettingPasswordViewController *lspVC = [[LYQLoginSettingPasswordViewController alloc] init];
        lspVC.navigationItem.title = @"手机注册";
        [self.navigationController pushViewController:lspVC animated:YES];
    }
    
}

#pragma mark - gesture action
- (void)endEditingTapAction {
    [self.view endEditing:YES];
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LYQLoginDefaultCellStyle style;
    
    if (indexPath.row == 0) {
        style = LYQLoginDefaultCellStyleNation;
    } else if (indexPath.row == 1) {
        style = LYQLoginDefaultCellStyleUsername;
    } else {
        style = LYQLoginDefaultCellStylePassword;
    }
    
    LYQLoginDefaultCell *cell = [[LYQLoginDefaultCell alloc] init];
    cell.cellStyle = style;
    cell.model = _model;
    
    return cell;
}


#pragma mark - tableview delegate
#pragma mark 分割线
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, kWScale(10), 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, kWScale(10), 0, 0)];
    }
}

#pragma mark 头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kWScale(25);
}

#pragma mark 单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kWScale(50);
}

#pragma mark 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        NSLog(@"选择国家或区域");
        LYQLoginNationListViewController *lnlVC = [[LYQLoginNationListViewController alloc] init];
        [self.navigationController pushViewController:lnlVC animated:YES];
    }
}

#pragma mark gesture delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return YES;
}


#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
